package com.itheima.common;


import com.itheima.execptiom.BusinessException;
import com.itheima.execptiom.SystemException;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//表示是一个Rest风格的Controller
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    private R doSQL(SQLIntegrityConstraintViolationException ex){
        if(ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }
        return R.error(ex.getMessage());
    }
    @ExceptionHandler(BusinessException.class)
    private R doBusinessException(BusinessException b){
        //交给后端程序员处理
        return R.error(b.getMessage());
    }

    @ExceptionHandler(SystemException.class)
    private R doSystemException(SystemException e){
        return R.error(e.getMessage());
    }




}
